#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::Auth;
use strict;
use warnings;
use base qw(App::Modular::Module);

print ":: Auth module loaded.\n";

# Events we want to receive.
sub events { return "PRIVMSG PART QUIT NOTICE"; }

sub help {
        "Administrative authorization script."
}

# Authorized users hash.
my %authed = ();

# Return 1 or 0 based on if the $from string is in our authed hash.
sub is_authed
{
        my ($self, $from) = @_;

        # Authorized.
        if ($authed{$from}) {
                return 1;
        }

        # Not authorized.
        return 0;
}

# NOTICE events.
sub event_notice
{
        event_privmsg(@_);
}

# PRIVMSG events.
sub event_privmsg
{
        shift;
        my $e = shift;

        # Query
        if ($e->to eq $e->cfg->nick) {
                my $help = $e->robo->mod("Helper") || return;
                my $nick = $help->parse_nick($e->from);

                # Login command.
                if ($e->data =~ /^login (.+):(.+)/) {
                        # I don't want logins to be logged.
                        $e->nolog(1);

                        my $user = `echo -n $1 | md5sum | cut -f 1 -d \" \"`;
                        my $pass = `echo -n $2 | md5sum | cut -f 1 -d \" \"`;

                        # Chomp user and pass.
                        chomp($user); chomp($pass);

                        # Check if this user is already logged in.
                        if ($authed{$e->from}) {
                                $e->s("NOTICE ".$nick." :You are already logged in!");
                                return;
                        }

                        # See if the user and pass matches a configured admin.
                        if (ref($e->cfg->get_cfg("admin")) eq 'ARRAY') {
                                foreach (@{$e->cfg->get_cfg("admin")}) {
                                        # Valid user.
                                        if ($_->{user} eq $user && $_->{pass} eq $pass) {
                                                $authed{$e->from} = 1;
                                                $e->s("NOTICE ".$nick." :You are now authorized.");
                                                $e->robo->log(":: ".$nick." logged in.");

                                                # Exit the loop.
                                                last;
                                        }
                                }
                        # Single admin block.
                        } else {
                                # Valid user.
                                if ($e->cfg->get_cfg("admin/user") eq $user && $e->cfg->get_cfg("admin/pass") eq $pass) {
                                        $authed{$e->from} = 1;
                                        $e->s("NOTICE ".$nick." :You are now authorized.");
                                        $e->robo->log(":: ".$nick." logged in.");

                                        # Exit the loop.
                                        last;
                                }
                        }
                # Logout command.
                } elsif ($e->data eq "logout") {
                        if ($authed{$e->from}) {
                                delete($authed{$e->from});
                                $e->robo->log(":: Removed ".$e->from." from the authorized users hash.");

                                $e->s("NOTICE ".$nick." :Successfully logged out.");
                        }
                }
        }
}

# PART events.
sub event_part
{
        shift;
        my $e = shift;

        # An authorized user parted, remove them from the authed list.
        if ($authed{$e->from}) {
                delete($authed{$e->from});
                $e->robo->log(":: Removed ".$e->from." from the authorized users hash.");
        }
}

# QUIT events.
sub event_quit
{
        shift;
        my $e = shift;

        # An authorized user parted, remove them from the authed list.
        if ($authed{$e->from}) {
                delete($authed{$e->from});
                $e->robo->log(":: Removed ".$e->from." from the authorized users hash.");
        }
}

1;
